<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form' );
	var $uses = array('User', 'Picture');

	function admin_index() {
		$this->User->recursive = 0;
		$this->set('users', $this->User->findAll());
	}

	function view($userId = null) {
		if (!$userId) {
			$this->Session->setFlash('Invalid userId');
			$this->redirect('/admin/users');
			return;
		}
		$user = $this->User->read(null, $userId);
		$this->set('user', $user);
		$this->set('referer', $this->referer());
	}

	function admin_view($userId = null) {
		$this->view($userId);
	}

	function admin_add() {
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash('The User has been saved');
				$this->redirect('/admin/users/');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_uploadPicture() {
		$this->set('userList', $this->User->getUserList());
		if (empty($this->data)) {
			$this->render();
		} else {
			if (!empty($this->params['form']) && is_uploaded_file($this->params['form']['File']['tmp_name'])) {
				$fileName = $this->params['form']['File']['tmp_name'];
				$fileData = fread(fopen($fileName, "r"), $this->params['form']['File']['size']);
				$userId = $this->data['User']['user_id'];
				if ($this->savePicture($userId, $fileData)) {
					$this->Session->setFlash('Pictures uploaded');
					$this->redirect('admin/users/');
				} else {
					$this->Session->setFlash('Please correct errors below');
				}
			} else {
				$this->Session->setFlash('Error uploading file');
			}
		}
	}

	private function savePicture($userId, $fileData) {
		$user = $this->User->read(null, $userId);
		$pictureId = $user['User']['picture_id'];
		$picture = array();
		$picture['Picture']['picture'] = $fileData;
		if ($pictureId == null) {
			$this->Picture->save($picture);
			$pictureId = $this->Picture->getLastInsertId();
			$user['User']['picture_id'] = $pictureId;
			$this->User->save($user);
		} else {
			$picture['Picture']['picture_id'] = $pictureId;
			$this->Picture->save($picture);
		}
		return 1;
	}

	function admin_uploadPictures() {
		if (empty($this->data)) {
			$this->render();
		} else {
			if (!empty($this->params['form']) && is_uploaded_file($this->params['form']['File']['tmp_name'])) {
				$fileData = fread(fopen($fileName, "r"), $this->params['form']['File']['size']);
				if ($this->Picture->savePictures($fileData)) {
					$this->Session->setFlash('Pictures uploaded');
					$this->redirect('users/');
				} else {
					$this->Session->setFlash('Please correct errors below');
				}
			}
		}
	}

	function admin_edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for User');
				$this->redirect('/users/index');
			}
			$this->data = $this->User->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash('The User has been saved');
				$this->redirect('/users/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function login() {
		if (empty($this->data)) {
			return;
		}

		$firstName = $this->data['User']['firstName'];
		$lastName = $this->data['User']['lastName'];
		$password = $this->data['User']['password'];
		$user = $this->User->checkUser($firstName, $lastName, $password);

		$this->Session->write('user', $user);
		$this->Session->write('isLoggedIn', true);
		$isAdmin = $user['User']['admin'];
		$this->Session->write('admin', $isAdmin);

		$this->redirect('/');
	}

	function logout() {
		$this->Session->del('user');
		$this->Session->del('password');
		$this->Session->write('isLoggedIn', false);

		$this->redirect('/');
	}
}
?>
